package com.example.caimogu.Goods2Json;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Created by Caimogu on 2016/4/21.
 */
public class String2Pic {

        private String fileString;
        public String2Pic(String fileString){
            this.fileString = fileString;
        }

        public void savePic() {

            File file = new File("/mnt/sdcard/Pictures/test.png");
            Log.i("caimogu","in-------");

            BufferedReader bufferedReader = null;
            BufferedWriter bufferedWriter = null;
            try{


                bufferedReader = new BufferedReader(new StringReader(fileString));
                bufferedWriter = new BufferedWriter(new FileWriter(file));
                char buf[] = new char[1024];         //字符缓冲区
                int len;
                while ((len = bufferedReader.read(buf)) != -1) {
                    bufferedWriter.write(buf, 0, len);
                }
                bufferedWriter.flush();
                bufferedReader.close();
                bufferedWriter.close();
                Log.i("caimogu","success.........");

            }catch(Exception e){
                e.printStackTrace();
            }


        }

    public void  string2File() {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        String  filePath = "/mnt/sdcard/Pictures/test.png";
        try {
            File distFile = new File(filePath);
            if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
            bufferedReader = new BufferedReader(new StringReader(fileString));
            bufferedWriter = new BufferedWriter(new FileWriter(distFile));
            char buf[] = new char[1024];         //字符缓冲区
            int len;
            while ((len = bufferedReader.read(buf)) != -1) {
                bufferedWriter.write(buf, 0, len);
            }
            bufferedWriter.flush();
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }




}
